package chapter14.Collection.Set.Exercise;

import java.util.HashSet;
import java.util.Objects;

/**
 * @Auther: Tx
 * @Date: 2022/5/11 23:02
 */
public class EX01 {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        Employee employee1 = new Employee("jack",21);
        Employee employee2 = new Employee("mike",22);
        Employee employee3 = new Employee("jack",21);
        HashSet set = new HashSet();
        set.add(employee1);
        set.add(employee2);
        set.add(employee3);
        System.out.println(set);
    }
}

class Employee {
    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    //重写equals方法
//   如果name和age值相同则返回相同的hashCode
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;//传入的类不同，或者是空。
        Employee employee = (Employee) o;//向下转型
        return age == employee.age &&
                name.equals(employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}